package com.wc.wcWebsite.controller;


import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wc.wcWebsite_api.CommomService;
import com.wc.wcWebsite_api.UserService;
import com.wc.wcWebsite_model.User;

@Controller
public class userController {
	private final static Logger logger=LoggerFactory.getLogger(userController.class);
	
	@Autowired
	private UserService userService;
	@Autowired
	private CommomService commomService;
	
	@RequestMapping("updateUser")
	@ResponseBody
	public String updateUser(User user, HttpServletRequest request){
		User user2=(User) request.getSession().getAttribute("userInfo");
		user.setUserId(user2.getUserId());
		logger.info("test");
		if (userService.updateUser(user)) {
			request.getSession().setAttribute("userInfo", user);
			return "0000";
		}else{
			return "0001";
		}
	}
	
	@RequestMapping("userEdit")
	public String userEdit(HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("userInfo");
		request.setAttribute("user", user);
		return "profile";
	}
	
	@RequestMapping("checkImg")
	@ResponseBody
	public String checkImg(HttpServletRequest request){
		String randCode=(String) request.getSession().getAttribute("code");
		String inputCode=request.getParameter("code");
		logger.info("inputcode="+inputCode);
		
		if(randCode.equalsIgnoreCase(inputCode) && !inputCode.isEmpty()){
			return "0000";
		}else{
			return "0001";
		}
	}
	
	@RequestMapping("getImgCode")
	public void getImgCode(HttpServletRequest request, HttpServletResponse response){
		response.setDateHeader("Expires", 0);  
        response.setHeader("Cache-Control",  
                "no-store, no-cache, must-revalidate");  
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");  
        response.setHeader("Pragma", "no-cache");  
        response.setContentType("image/jpeg");
        
        Map<String, Object> map=commomService.getRandCode();
		String randCode=(String) map.get("randCode");
		byte[] imgByteArray=(byte[]) map.get("imgByteArray");
		
		request.getSession().setAttribute("code", randCode);
		Cookie cookie=new Cookie("code", randCode);
		response.addCookie(cookie);
		
		ServletOutputStream out=null;
		try {
			out = response.getOutputStream();
			out.write(imgByteArray);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@RequestMapping("cache")
	@ResponseBody
	public String cache(){
		User user=userService.findUserByName("wc");
		System.out.println("name="+user.getUsername());
		return "cache";
	}
}
